package thread;

//通过懒汉模式构造单例模式

class SingletonLazy{
    private static volatile SingletonLazy instance = null;
    private static Object locker = new Object();

    public static SingletonLazy getInstance(){
        if(instance == null){
            synchronized (locker){
                if (instance == null){
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    public synchronized static SingletonLazy getInstance2(){
        if (instance == null){
            instance = new SingletonLazy();
        }
        return instance;
    }

    private SingletonLazy(){}
}


public class Demo28 {
    public static void main(String[] args) {
        SingletonLazy singletonLazy1 = SingletonLazy.getInstance2();
        SingletonLazy singletonLazy2 = SingletonLazy.getInstance();
        System.out.println(singletonLazy1 == singletonLazy2);
    }
}
